package com.kaishengit.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Book;
import com.kaishengit.entity.BookType;
import com.kaishengit.entity.Publish;
import com.kaishengit.service.BookService;
import com.kaishengit.service.BookTypeService;
import com.kaishengit.service.PublishService;
import com.kaishengit.util.web.ServletUtil;

public class BookNew extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PublishService publishService = new PublishService();
		BookTypeService bookTypeService = new BookTypeService();
		
		List<Publish> publishList = publishService.findAll();
		List<BookType> bookTypeList = bookTypeService.findAll();
		
		request.setAttribute("publishList", publishList);
		request.setAttribute("bookTypeList", bookTypeList);
		
		ServletUtil.forword(request, response, "admin/book-new");
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Map<String, String> map = ServletUtil.uploadFile(request);
		
		String bookName = map.get("bookname");
		String pubid = map.get("pubid");
		String author = map.get("author");
		String price = map.get("price");
		String salesprice = map.get("salesprice");
		String typeid = map.get("typeid");
		String pubdate = map.get("pubdate");
		String pagenum = map.get("pagenum");
		String num = map.get("num");
		String dangenum = map.get("dangenum");
		String content = map.get("content");
		String authorcontent = map.get("authorcontent");
		String preface = map.get("preface");
		String fileName = map.get("fileName");
		
		Book book = new Book();
		book.setAuthorcontent(authorcontent);
		book.setBookauthor(author);
		book.setBookname(bookName);
		book.setBooknum(Integer.parseInt(num));
		book.setBooktypeId(typeid);
		book.setContent(content);
		book.setDangenum(Integer.parseInt(dangenum));
		book.setPagenum(Integer.parseInt(pagenum));
		book.setPic(fileName);
		book.setPreface(preface);
		book.setPublishdate(pubdate);
		book.setPrice(Float.parseFloat(price));
		book.setPublishId(pubid);
		book.setSalesprice(Float.parseFloat(salesprice));
		
		
		BookService bookService = new BookService();
		bookService.save(book);
		
		ServletUtil.redirect(response, "/admin/book.jspx");
		
		
	}

}
